package parser.nodes;

import java.util.HashMap;
import java.util.Iterator;

import parser.AstNode;
import parser.Memory;
import parser.StringMatch;
import parser.StringMatchSet;

public class ExpressionListNode extends AstNode {

	@Override
	public Object execute(HashMap<String, Object> memory) {
		Object lval = null;
		Object rval = null;
		if(getLchild()!=null)
			lval = lchild.execute(memory);
		this.printVal(memory, lval);
		if(getRchild()!=null)
			rval = rchild.execute(memory);
		//this.printVal(memory, rval);
		return this.executeNode(memory, lval, rval);
	}
	
	@Override
	protected Object executeNode(HashMap<String,Object> mem, Object lval, Object rval) {
		
		return null;
	}
	public void printVal(HashMap<String,Object> mem, Object val)
	{
		if(val!=null)
		{
			if(val instanceof String)
			{
				if(mem.containsKey(val))
					val = mem.get((String)val);
				else
				{	System.out.println(val); return;}
			}
			if(val instanceof StringMatchSet)
			{
				Iterator<StringMatch> iter = ((StringMatchSet)val).matchSet.iterator();
				while(iter.hasNext())
				{
					StringMatch sm = iter.next();
					System.out.println("String: "+sm.value+" FileName: "+sm.fileName+" Line#: "+sm.lineNumber+" StartIndex: "+sm.startIndex+" EndIndex: "+sm.endIndex);
				}
			}
			else if(val instanceof Integer)
			{
				System.out.println("Number of String Matches:"+val);
			}
			else if((val instanceof String)&&(!((String)val).equals("")))
			{
				
				System.out.println("MaxFreqString: "+val);
			}
			
		}
	}
}
